package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGChangeWork;

/**
 * <p>Title: ServiceZMCP01.java</p>
 * <p>Description: 交接班记录主表</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author张坤
 * @version 1.0
 * <pre>Histroy:
 *          2016-03-21 下午03:27:32 张坤  Create
 *</pre>
 */
public class ServiceZMCG40  extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCP01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:26:45张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCP01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:26:59张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {

		EiInfo outInfo = inInfo;

		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
	    outInfo = super.query(inInfo, "ZMCGChangeWork.query", new ZMCGChangeWork());

	    outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", fdMiningId, true));	
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[]{"0", "1"}, new String[]{"处理中", "已解决"}, true));//状态
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:31:06张坤
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		//校验是否重复
		EiInfo out=new EiInfo(); 
		ZMCGChangeWork _dom = new ZMCGChangeWork();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {

			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_CHANGEWORK"));
			_dom.setFdCreateUserId((String) UserSession.getUserId());
			_dom.setFdCreateUser((String) UserSession.getInSessionProperty("UserCname"));
			_dom.setFdCreateDepartment((String) UserSession.getInSessionProperty("orgName"));
			_dom.setFdCreateDepartmentId((String) UserSession.getInSessionProperty("orgCode"));
			_dom.setFdMiningId((String) UserSession.getInSessionProperty("miningCode"));
			_dom.setFdMiningName((String) UserSession.getInSessionProperty("miningName"));
			
			//如果有附件上传功能的模块，需要增加以下生成docid方法。生成规则调用统一的方法。修改时不用管docid，也就是docid一旦生成不能修改。除非删除
			_dom.setFdDocId(StaticTools.getDocId());
			this.dao.insert("ZMCGChangeWork.insert",_dom);

		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:59:23张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {				
		EiInfo out=new EiInfo(); 
		ZMCGChangeWork _dom = new ZMCGChangeWork();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdCreateUserId((String) UserSession.getUserId());
			_dom.setFdCreateUser((String) UserSession.getInSessionProperty("UserCname"));
			this.dao.update("ZMCGChangeWork.update", _dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;    
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:12:10张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGChangeWork _dom = new ZMCGChangeWork();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String>	params = new HashMap<String, String>();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdChangeId", _dom.getFdKeyId());
			
			boolean validate = this.validate(_dom, "ZMCGChangeWorkDetail.validate", params);
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("该问题包含跟踪情况，不能删除！");
				return out;
			}
			this.dao.delete("ZMCGChangeWork.delete", _dom);
		}
		
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
		
	public EiInfo resolve(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		ZMCGChangeWork _dom = new ZMCGChangeWork();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		Map<String, String>	params = new HashMap<String, String>();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdChangeId", _dom.getFdKeyId());
			boolean validate = this.validate(_dom, "ZMCGChangeWorkDetail.validate", params);
			if(validate) {
				this.dao.update("ZMCGChangeWork.resolve", _dom);
			}
			else {	
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("该问题没有包含跟踪情况，不能更改状态为已解决！");
				return out;
			}
			
		}
		EiInfo outInfo = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
	
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 对主表记录新增，删除，上报前的后台校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:40:23张坤
	 * </p>
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCGChangeWork _dom,String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0 ) {
			return true;
		}		
		return false;
	}
	
}
